package com.evildog.workspace.life.application.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.evildog.workspace.common.object.PageQuery;
import com.evildog.workspace.common.result.PagedResult;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.evildog.workspace.dependencies.mybatis.object.QueryConditionDTO;
import com.evildog.workspace.dependencies.mybatis.util.QueryConditionUtils;
import com.evildog.workspace.life.application.bo.AlbumBO;
import com.evildog.workspace.life.application.bo.AlbumPageBO;
import com.evildog.workspace.life.application.convert.AlbumConvert;
import com.evildog.workspace.life.application.dto.AlbumDTO;
import com.evildog.workspace.life.application.dto.AlbumPageDTO;
import com.evildog.workspace.life.application.entity.Album;
import com.evildog.workspace.life.application.entity.AlbumPageDO;
import com.evildog.workspace.life.application.mapper.AlbumMapper;
import com.evildog.workspace.life.application.mapper.AlbumTagMapper;
import com.evildog.workspace.life.application.service.AlbumService;
import com.evildog.workspace.life.application.service.PhotoService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

@Service
@AllArgsConstructor
public class AlbumServiceImpl extends ServiceImpl<AlbumMapper, Album> implements AlbumService {

    private final AlbumTagMapper albumTagMapper;
    private final PhotoService photoService;

    @Override
    public PagedResult<AlbumPageBO> getPageList(PageQuery query, List<QueryConditionDTO> queryConditions, AlbumPageDTO pageDTO) {
        IPage<AlbumPageDO> page = this.baseMapper.getPageList(
                QueryConditionUtils.getPagination(query),
                queryConditions,
                pageDTO);
        return AlbumConvert.INSTANCE.convert(page);
    }

    @Override
    public AlbumBO getDetailById(Long id) {
        Album album = this.getById(id);
        Assert.notNull(album, "album is null!");
        return AlbumConvert.INSTANCE.convert(album);
    }

    @Override
    public boolean save(AlbumDTO dto){
        Album newAlbum = AlbumConvert.INSTANCE.convert(dto);
        return save(newAlbum);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean remove(Long id) {
        if(removeById(id)){
            // 删除相册标签

            // 删除相册下所有相片

            return true;
        }else {

            return false;
        }
    }

    @Override
    public boolean updateById(Long id, AlbumDTO dto){
        Album oldAlbum = getById(id);
        Assert.notNull(oldAlbum, "album is null!");
        Album newAlbum = AlbumConvert.INSTANCE.convert(dto);
        newAlbum.setId(id);
        return updateById(newAlbum);
    }
}
